<template>
  <view>
    <!-- 搜索框 -->
   <view class="search-box">
     <Search @click="goToSearch"></Search>
   </view>
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :circular="true">
      <swiper-item v-for="img in swiperList" :key="img.goods_id">
        <navigator class="swiper-item" :url=" '/subpkg/goods_detail/goods_detail?goods_id=' + img.goods_id " open-type="navigate">
          <image :src="img.image_src" mode="widthFix"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="nav">
      <view class="nav-item" v-for="(nav, index) in navList" :key="index" @click="navClickHandler(nav)">
        <image :src="nav.image_src" mode="widthFix"></image>
      </view>
    </view>
    <!-- 楼层 -->
    <view class="floor">
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <view class="floor-item-title">
          <image :src="item.floor_title.image_src" mode="widthFix"></image>
        </view>
        <view class="floor-item-box">
          <navigator class="floor-left-box" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" mode="widthFix" :style="{width: item.product_list[0].image_width + 'rpx'}"></image>
          </navigator>
          <view class="floor-right-box">
            <navigator class="right-img-item" v-for="(itemImg, i) in item.product_list" :key="i" v-show="i !== 0" :url="itemImg.url">
              <image :src="itemImg.image_src" :style="{width: itemImg.image_width + 'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import badegMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins: [badegMix],
    data() {
      return {
        // 轮播图数据
        swiperList: [],
        // 分类导航数据
        navList: [],
        // 楼层数据
        floorList: []
      };
    },
    onLoad() {
      this.getSwiperList();
      this.getCategoryList();
      this.getFloorList();
    },
    methods: {
      // 获取轮播图数据
      async getSwiperList() {
        const result = await uni.$http.get('/api/public/v1/home/swiperdata');
        if (result.data.meta.status !== 200) return uni.$showMsg();
        this.swiperList = result.data.message;
      },
      // 获取分类导航数据
      async getCategoryList() {
        const result = await uni.$http.get('/api/public/v1/home/catitems');
        if (result.data.meta.status !== 200) return uni.$showMsg();
        this.navList = result.data.message;
      },
      // 分类导航跳转
      navClickHandler(nav) {
        if (nav.name === "分类") {
          uni.switchTab({
            url: "/pages/category/category"
          })
        } else uni.$showMsg("暂未开发");
      },
      // 获取楼层数据
      async getFloorList() {
        const result = await uni.$http.get('/api/public/v1/home/floordata');
        if (result.data.meta.status !== 200) return uni.$showMsg();
        // 处理楼层数据（图片跳转地址）
        result.data.message.forEach(floor => {
          floor.product_list.forEach(info => {
            info.url = "/subpkg/goods_list/goods_list?" + info.navigator_url.split('?')[1];
          })
        })
        this.floorList = result.data.message;
      },
      // 跳转到搜索页面
      goToSearch() {
        uni.navigateTo({
          url: "/subpkg/search/search"
        })
      }
    }
  }
</script>

<style lang="scss">
  // 搜索框
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }
  // 轮播图
  swiper {
    width: 100%;
    height: 330rpx;
    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }
  // 分类导航栏
  .nav {
    display: flex;
    justify-content: space-around;
    margin: 15px 0;
    image {
      width: 128rpx;
      height: 140rpx;
    }
  }
  // 楼层
  .floor-item {
    width: 100%;
    .floor-item-title {
      width: 100%;
      height: 60rpx;
      // background-color: pink;
      image {
        width: 100%;
        height: 100%;
      }
    }
    .floor-item-box {
      display: flex;
      padding-left: 10rpx;
      .floor-right-box {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
      }
    }
  }
</style>
